using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Data.Common;
using CMS.Web;
using System.Collections.Generic;
using ProjectBase.Utils;

namespace CMS.Web.ListManager
{
    public partial class AddBaiVietTheLoai : BaseUserControl, IAddBaiVietTheLoaiView
    {
        private AddBaiVietTheLoaiPresenter presenter;
        private BaiVietTheLoai TheLoaiCha;
        private IList<ChucNang> _ChucNangList;
        string maBVTL = "";

        public AddBaiVietTheLoai()
        {
            this.presenter = new AddBaiVietTheLoaiPresenter(this, NHibernateDaoFactory.Instance.GetBaiVietTheLoaiDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            maBVTL = CRequest.GetQueryUri("MaBVTL", "0");
            if (!maBVTL.Equals("0"))
            {
                this.TheLoaiCha = new BaiVietTheLoaiDao().GetById(Convert.ToInt32(maBVTL));
                this.trChucNang.Visible = false;
            }
            else
            {
                this.trChucNang.Visible = true;
                this.trBVTLLookup.Visible = false;
            }
            // For Ajax
            this.RaiseClientEvent();
            if (!this.IsPostBack)
            {
                if (!maBVTL.Equals("0"))
                {
                    this.txtSTT.Text = Convert.ToString(presenter.GetMaxSTT(this.TheLoaiCha.ID));
                    this.lblBVTLLookup.Text = this.TheLoaiCha.TenTheLoai;
                }
            }
            this.btnOK.Attributes.Add("onclick", "return CheckInput(0);");
            this.btnOKC.Attributes.Add("onclick", "return CheckInput(1);");

            this.drpChucNang.DataSource = this.ChucNangList;
            this.drpChucNang.DataBind();
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.AddLinhVucPresenter));
        }        

        protected void btnOK_Click(object sender, EventArgs e)
        {
            BaiVietTheLoai theloai = presenter.AddObj(Page.IsValid);
            Response.Redirect(BaseUrl + "&action=VIEW&MaBVTL=" + theloai.ID);
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            BaiVietTheLoai theloai = presenter.AddObj(Page.IsValid);
            this.maBVTL = theloai.ID.ToString();
            this.txtName.Text = "";
            this.txtName.Focus();            
            this.txtSTT.Text = Convert.ToString(presenter.GetMaxSTT(this.TheLoaiCha.ID));
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW&MaBVTL=" + this.maBVTL);
        }

        #region IAddBaiVietTheLoaiView Members

        public string Message
        {
            set
            { 
                //lblMessage.Text = value; 
            }
        }

        public BaiVietTheLoai BaiVietTheLoaiToAdd
        {
            get
            {
                BaiVietTheLoai obj = new BaiVietTheLoai();
                obj.TenTheLoai = txtName.Text.Trim();
                if ((txtSTT.Text.Trim() != null) && (!txtSTT.Text.Trim().Equals("")))
                {
                    obj.ThuTu = Convert.ToByte(txtSTT.Text.Trim());
                }
                obj.TheLoaiCha = this.TheLoaiCha;
                obj.TrangThai = Convert.ToBoolean(radStatus.SelectedValue);
                if (this.TheLoaiCha == null)
                {
                    obj.ChucNangLookup = new ChucNangDao().GetById(Convert.ToInt32(this.drpChucNang.SelectedValue));
                }
                else 
                {
                    obj.ChucNangLookup = this.TheLoaiCha.ChucNangLookup;
                }

                return obj;
            }
        }         

        public IList<ChucNang> ChucNangList
        {
            get
            {
                return this._ChucNangList;
            }
            set
            {
                this._ChucNangList = value;
            }
        }

        #endregion
    }
}